const LINE_STYLE = {
  Color: 6,
  // 设置线型号
  LinStyleID: 0,
  LinStyleID2: 17,
  LinWidth: 0.1,
  Xscale: 10,
  Yscale: 10,
}
/**
 * @param {Array} position  [[114,30],[104.07,30.58]]
 * @param {array} attr [{key:'name,type:'string',value:'武汉'}]
 * @param {object} service {docName,layerId} docName 地图文档服务名 layerId 要素所在的图层
 * @param {object} docLayer 实例化的文档图层
 */

export class LineString {
  static addLine({ position, attr, service, docLayer }) {
    var pointObj = new Array()
    position.forEach((item) => {
      pointObj.push(new Zondy.Object.Point2D(item[0], item[1]))
    })
    //构成折线的弧段
    var gArc = new Zondy.Object.Arc(pointObj)
    //构成线的折线
    var gAnyLine = new Zondy.Object.AnyLine([gArc])
    //设置线要素的几何信息
    var gline = new Zondy.Object.GLine(gAnyLine)
    //设置要素的几何信息
    var fGeom = new Zondy.Object.FeatureGeometry({ LinGeom: [gline] })
    /* 构建样式信息 */
    var clineInfo = new Zondy.Object.CLineInfo(LINE_STYLE)
    //设置要素的图形参数信息
    var graphicInfo = new Zondy.Object.WebGraphicsInfo({
      InfoType: 2,
      LinInfo: clineInfo,
    })
    var attValue = attr.map((item) => item.value)

    //创建一个线要素
    var newFeature = new Zondy.Object.Feature({
      fGeom: fGeom,
      GraphicInfo: graphicInfo,
      AttValue: attValue,
    })
    //设置要素为线要素
    newFeature.setFType(2)
    /* 2. 构建要素集 */
    //创建一个要素数据集
    var featureSet = new Zondy.Object.FeatureSet()
    var fldNumber = attr.length //属性个数
    var fldName = attr.map((item) => item.key)
    var fldType = attr.map((item) => item.type)
    //创建属性结构设置对象
    var cAttStruct = new Zondy.Object.CAttStruct({
      FldName: fldName,
      FldNumber: fldNumber,
      FldType: fldType,
    })
    //设置要素数据集的树形结构
    featureSet.AttStruct = cAttStruct
    //将添加的线要素添加到属性数据集中
    featureSet.addFeature(newFeature)
    /* 3. 调用服务，实现地图文档的编辑 */
    var editDocFeature = new Zondy.Service.EditDocFeature(
      service.docName,
      service.layerId,
      {
        ip: 'localhost',
        port: '6163', //访问IGServer的端口号，.net版为6163，Java版为8089
      }
    )
    editDocFeature.add(featureSet, this.onAddLineSuccess(docLayer))
  }
  static onAddLineSuccess(docLayer) {
    return function (data) {
      if (data) {
        alert('添加线要素成功！')
        docLayer.refresh()
      } else {
        alert('添加线要素失败！')
      }
    }
  }
}
